/*
 * Period.java
 *
 * Creada el 1/02/2012, 02:52:47 PM
 *
 * Clase Java desarrollada por Edgar Ramos para 7i Software Factory el día 1/02/2012
 *
 * Para informacion sobre el uso de esta clase, asi como bugs, actualizaciones, o mejoras enviar un mail a
 * eramos@7i.com.mx
 *
 * Desarrollado por la célula de desarrollo de Zacatecas
 *
 */
package com.grupoquark.cic.model.period;

import com.grupoquark.cic.model.enums.Semester;
import com.grupoquark.cic.model.person.Child;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.apache.log4j.Logger;

/**
 * @author Edgar Ramos
 * @version 1.0
 * @author-mail eramos@7i.com.mx
 * @date 1/02/2012
 */
@Entity
@Table(name = "periods")
public class Period implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private Semester semester;
    @Column(name = "period_year")
    private int year;
    @Column(name = "registered_children")
    @ManyToMany
    private List<Child> registeredChildren;
    private static final transient Logger log = Logger.getLogger(Period.class);

    protected Period()
    {
    }

    public Period(Semester semester, int year)
    {
        this.semester = semester;
        this.year = year;
    }

    public Period(Date date)
    {
        this.year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        this.semester = determineSemester(date);
    }

    public long getId()
    {
        return id;
    }

    protected void setId(long id)
    {
        this.id = id;
    }

    public List<Child> getRegisteredChildren()
    {
        return registeredChildren;
    }

    public void setRegisteredChildren(List<Child> registeredChildren)
    {
        this.registeredChildren = registeredChildren;
    }

    public Semester getSemester()
    {
        return semester;
    }

    public void setSemester(Semester semester)
    {
        this.semester = semester;
    }

    public int getYear()
    {
        return year;
    }

    public void setYear(int year)
    {
        this.year = year;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Period other = (Period) obj;
        if (this.id != other.id)
        {
            return false;
        }
        if (this.semester != other.semester)
        {
            return false;
        }
        if (this.year != other.year)
        {
            return false;
        }
        if (this.registeredChildren != other.registeredChildren && (this.registeredChildren == null || !this.registeredChildren.equals(other.registeredChildren)))
        {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hash = 3;
        hash = 43 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 43 * hash + (this.semester != null ? this.semester.hashCode() : 0);
        hash = 43 * hash + this.year;
        hash = 43 * hash + (this.registeredChildren != null ? this.registeredChildren.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString()
    {
        return "Period{" + "id=" + id + ", semester=" + semester + ", year=" + year + ", registeredChildren=" + registeredChildren + '}';
    }

    private Semester determineSemester(Date date)
    {
        String monthNumberString = new SimpleDateFormat("MM").format(date);
        int monthNumber = Integer.parseInt(monthNumberString);
        if (log.isDebugEnabled())
        {
            log.debug("Date: " + date);
            log.debug("Month number: " + monthNumberString);
        }
        return monthNumber < 6 ? Semester.FIST_SEMESTER : Semester.SECOND_SEMESTER;

    }
}
